package online.westonline.com.westonline.adapter.meeting;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.easy.west.westclient.api.meeting.bean.MeetRoomBean;

import net.wujingchao.android.view.SimpleTagImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.ui.OrederMeetActivity;

/**
 * 会议室列表视图
 * Created by xiatao on 2018/8/21.
 */

public class MeetListAdpter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<MeetRoomBean> roomBeanList = new ArrayList<>();
    private onMeetingListener onMeetingListener;

    public void setOnMeetingListener(MeetListAdpter.onMeetingListener onMeetingListener) {
        this.onMeetingListener = onMeetingListener;
    }

    /**
     * 设置数据源
     *
     * @param roomBeanList
     */
    public void setDatas(List<MeetRoomBean> roomBeanList) {
        this.roomBeanList = roomBeanList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.meeting_item, parent, false);
        return new MeetingRoomViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MeetingRoomViewHolder)
        {
            final MeetRoomBean roomBean = roomBeanList.get(position);
            MeetingRoomViewHolder roomViewHolder = (MeetingRoomViewHolder) holder;
            if (TextUtils.isEmpty(roomBean.getImg()))
            {
                //空的图片url
                roomViewHolder.imgMeetingRoom.setBackgroundResource(R.mipmap.book_bg);
            }
            else
            {
                Glide.with(roomViewHolder.itemView).load(roomBean.getImg()).into(roomViewHolder.imgMeetingRoom);
            }
            //是否是vip
            if (roomBean.isVip())
            {
                roomViewHolder.imgMeetingRoom.setTagEnable(true);
            }
            else
            {
                roomViewHolder.imgMeetingRoom.setTagEnable(false);
            }
            //是否被预约(过后添加字段)

            roomViewHolder.tvMeetingName.setText(roomBean.getName());
            if (roomBean.getSize()==1)
            {
                roomViewHolder.tvMeetingType.setText("大型");
            }
            if (roomBean.getSize()==2)
            {
                roomViewHolder.tvMeetingType.setText("中型及以下");
            }
        /**
            roomViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle b = new Bundle();
                    b.putSerializable("data",roomBean);
                    Intent i = new Intent(holder.itemView.getContext(), OrederMeetActivity.class);
                    i.putExtras(b);
                    holder.itemView.getContext().startActivity(i);
                }
            });
        **/

        }
    }

    @Override
    public int getItemCount() {
        return roomBeanList == null ? 0 : roomBeanList.size();
    }

    /**
     * 会议室viweholder
     */
    class MeetingRoomViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_meeting_room)
        SimpleTagImageView imgMeetingRoom;
        @BindView(R.id.ra_img_bg)
        RelativeLayout raImgBg;
        @BindView(R.id.tv_meeting_name)
        TextView tvMeetingName;
        @BindView(R.id.tv_meeting_type)
        TextView tvMeetingType;
        @BindView(R.id.tv_meeting_time)
        TextView tvMeetingTime;
        @BindView(R.id.tv_meeting_state)
        TextView tvMeetingState;

        public MeetingRoomViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null!=onMeetingListener)
                    {
                        onMeetingListener.onclick(getLayoutPosition(),roomBeanList.get(getLayoutPosition()));
                    }
                }
            });
        }
    }

    public interface onMeetingListener
    {
        void onclick(int pos,MeetRoomBean roomBean);
    }

}
